package cn.xinfei.xdecision.engine.api;

import cn.xinfei.xdecision.common.utils.util.JsonUtil;
import cn.xinfei.xdecision.common.utils.util.JsonUtils;
import cn.xinfei.xdecision.engine.runner.context.PipelineContextHolder;
import cn.xinfei.xdecision.engine.runner.context.XDecisionContext;
import cn.xinfei.xdecision.engine.runner.datax.*;
import cn.xinfei.xdecision.model.DecisionDataxApiRequest;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@RestController
@RequestMapping("engine/data")
public class XDecisionDataApi {

    @Autowired
    private DataCreditApplyHandler dataCreditApplyHandler;

    @Autowired
    private DataCreditFetchHandler dataCreditFetchHandler;

    @Autowired
    private DataPreCollectHandler dataPreCollectHandler;

    @Autowired
    private DataRealCollectHandler dataRealCollectHandler;


    /**
     * 预采集接口
     {
     "inputParams": {
     "app_id": "111",
     "credit_channel": "huarong",
     "name": "柳争",
     "type": "1",
     "stage": "1",
     "mobile": "15510702998",
     "id_card_number": "211402198212025025"
     },
     "fields": [
     "huarong2_22",
     "huarong2_12",
     "id_card_number"
     ],
     "requestId": "age"
     }
     * @param apiRequest
     * @return
     */
    @RequestMapping(value = "/pre", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "预采集接口")
    public Object pre(@RequestBody DecisionDataxApiRequest apiRequest) {
        log.info("pre request:{}", JsonUtil.toJson(apiRequest));
        Object execute = null;
        try {
            XDecisionContext.destroy();
            Map<String, Object> inputParams = apiRequest.getInputParams();
            String requestId = apiRequest.getRequestId();
            PipelineContextHolder.setRequestId(requestId);
            PipelineContextHolder.setDecisionId(requestId);
            PipelineContextHolder.putInput(inputParams);
            execute = dataPreCollectHandler.queryFieldValue(apiRequest.getFields(), RiskDataApiAdapter.CollectTypeEnum.PRE.toString());
            ConcurrentHashMap<String, Object> context = PipelineContextHolder.getPipelineContext().getContext();
            return context;
        } catch (Exception e) {
            log.error("预采集接口，error={},apiRequest={}", e.getMessage(), JsonUtil.toJson(apiRequest), e);
        }
        return execute;
    }


    /**
     * 实时采集接口
     *
     * @param apiRequest
     * @return
     */
    @ApiOperation(value = "实时采集接口")
    @RequestMapping(value = "/real", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Object real(@RequestBody DecisionDataxApiRequest apiRequest) {
        log.info("real request:{}", JsonUtil.toJson(apiRequest));
        Object execute = null;
        try {
            XDecisionContext.destroy();
            Map<String, Object> inputParams = apiRequest.getInputParams();
            String requestId = apiRequest.getRequestId();
            PipelineContextHolder.setRequestId(requestId);
            PipelineContextHolder.setDecisionId(requestId);
            PipelineContextHolder.putInput(inputParams);
            execute = dataRealCollectHandler.queryFieldValue(apiRequest.getFields(), RiskDataApiAdapter.CollectTypeEnum.REAL.toString());
            ConcurrentHashMap<String, Object> context = PipelineContextHolder.getPipelineContext().getContext();
            return context;
        } catch (Exception e) {
            log.error("实时采集接口，error={},apiRequest={}", e.getMessage(), JsonUtils.toJsonString(apiRequest), e);
        }
        return execute;
    }


    /**
     * 实时采集接口
     * 人行申请接口
     *
     * @param apiRequest
     * @return
     */
    @ApiOperation(value = "人行申请接口")
    @RequestMapping(value = "/apply", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Object apply(@RequestBody DecisionDataxApiRequest apiRequest) {
        log.info("apply request:{}", JsonUtils.toJsonString(apiRequest));
        Object execute = null;
        try {
            XDecisionContext.destroy();
            Map<String, Object> inputParams = apiRequest.getInputParams();
            String requestId = apiRequest.getRequestId();
            PipelineContextHolder.setRequestId(requestId);
            PipelineContextHolder.setDecisionId(requestId);
            PipelineContextHolder.putInput(inputParams);
            execute = dataCreditApplyHandler.queryFieldValue(apiRequest.getFields(), RiskDataApiAdapter.CollectTypeEnum.CREDIT_APPLY.toString());
            ConcurrentHashMap<String, Object> context = PipelineContextHolder.getPipelineContext().getContext();
            return context;
        } catch (Exception e) {
            log.error("人行申请接口，error={},apiRequest={}", e.getMessage(), JsonUtils.toJsonString(apiRequest), e);
        }
        return execute;
    }


    /**
     * 人行取数接口
     *
     * @param apiRequest
     * @return
     */
    @ApiOperation(value = "人行取数接口")
    @RequestMapping(value = "/fetch", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Object fetch(@RequestBody DecisionDataxApiRequest apiRequest) {
        log.info("fetch request:{}", JsonUtils.toJsonString(apiRequest));
        Object execute = null;
        try {
            XDecisionContext.destroy();
            Map<String, Object> inputParams = apiRequest.getInputParams();
            String requestId = apiRequest.getRequestId();
            PipelineContextHolder.setRequestId(requestId);
            PipelineContextHolder.setDecisionId(requestId);
            PipelineContextHolder.putInput(inputParams);
            execute = dataCreditFetchHandler.queryFieldValue(apiRequest.getFields(), RiskDataApiAdapter.CollectTypeEnum.CREDIT_FETCH.toString());
            ConcurrentHashMap<String, Object> context = PipelineContextHolder.getPipelineContext().getContext();
            return context;
        } catch (Exception e) {
            log.error("人行取数接口，error={},apiRequest={}", e.getMessage(), JsonUtils.toJsonString(apiRequest), e);
        }
        return execute;
    }
}
